package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.constant.jcy.JcyLxjhConstant;
import com.yuncheng.spcyApi.constant.jcy.JcydjbConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyRsHcy;
import com.yuncheng.spcyApi.entity.SpcyRsJcysfbgjl;
import com.yuncheng.spcyApi.mapper.SpcyRsHcyMapper;
import com.yuncheng.spcyApi.mapper.SpcyRsJcysfbgjlMapper;
import com.yuncheng.spcyApi.service.ISpcyRsJcysfbgjlService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.OrderItemUtils;
import com.yuncheng.spcyApi.utils.StringPatternUtils;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.DateConversionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 检查员身份变更记录
 * @Author: yuncheng
 * @Date: 2024-01-06
 * @Version: V1.0
 */
@Service
public class SpcyRsJcysfbgjlServiceImpl extends BaseServiceImpl<SpcyRsJcysfbgjlMapper, SpcyRsJcysfbgjl> implements ISpcyRsJcysfbgjlService {

    @Resource
    private SpcyRsJcysfbgjlMapper spcyRsJcysfbgjlMapper;

    @Resource
    @Lazy
    private SpcyRsHcyMapper spcyRsHcyMapper;

    @Override
    public Page<SpcyRsJcysfbgjl> pageList(SpcyRsJcysfbgjl query, PageVo pageVo, OrderItemVo orderItemVo) {
        Page<SpcyRsJcysfbgjl> page = new Page<>(pageVo.getPageNo(),pageVo.getPageSize());

        LambdaQueryWrapper<SpcyRsJcysfbgjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(query.getFuserid()), SpcyRsJcysfbgjl::getFuserid, query.getFuserid());

        if (StringUtils.isBlank(orderItemVo.getOrderItem())){
            queryWrapper.orderByDesc(SpcyRsJcysfbgjl::getCreateTime);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        return spcyRsJcysfbgjlMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SpcyRsJcysfbgjl> selectAllList(SpcyRsJcysfbgjl query, OrderItemVo orderItemVo) {
        LambdaQueryWrapper<SpcyRsJcysfbgjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(query.getFuserid()), SpcyRsJcysfbgjl::getFuserid, query.getFuserid());

        if (StringUtils.isBlank(orderItemVo.getOrderItem())){
            queryWrapper.orderByDesc(SpcyRsJcysfbgjl::getCreateTime);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        return spcyRsJcysfbgjlMapper.selectList(queryWrapper);
    }

    @Override
    public void initLxJcysgbgjl(String userid, String fjcyid, String fjcylb, String fjcyxl, String fssk, String fzytc,String fpid,String fwjh,String sfsqzh) {
        SpcyRsJcysfbgjl jcysfbgjl = new SpcyRsJcysfbgjl();
        jcysfbgjl.setId(GetUuIdUtils.ReplaceUuId());
        jcysfbgjl.setFjcyid(fjcyid);
        jcysfbgjl.setFuserid(userid);
        jcysfbgjl.setFpid(fpid);
        jcysfbgjl.setFsfsqzh(sfsqzh);
        jcysfbgjl.setFbglx(JcyLxjhConstant.JCY_SFBG_BGLX_LX);
        jcysfbgjl.setFxllx(SpcyConstant.getJcyXllxByJcylb(fjcylb));
        jcysfbgjl.setFzytc(fzytc);

        if (StringUtils.isNotBlank(fjcyxl) && StringUtils.isNotBlank(fjcylb)){
            if (!fjcyxl.equals(fjcylb)){
                fjcylb = fjcylb+"("+fjcyxl+")";
            }
        }

        /*if (StringUtils.isNotBlank(fjcyxl) && !fjcylb.equals(fjcyxl)){
            fjcylb = fjcylb + "("+fjcyxl+")";
        }*/
        jcysfbgjl.setFjcylx(fjcylb);
        jcysfbgjl.setFwjh(fwjh);
        jcysfbgjl.setFbgsj(DateConversionUtils.DateToDays(new Date()));

        String fbgyy = "";

        fbgyy += "无"+"-重新登记-" + fssk;

        /*String fbgyy = "（"+jcysfbgjl.getFbglx()+"）认证检查员类别-" + fjcyxl + "；";
        if (StringUtils.isNotBlank(fssk)){
            fbgyy += "身份：【无】晋升为【" + fssk + "】;";
        }
        if (StringUtils.isNotBlank(fzytc)){
            fbgyy += "特长：" + fzytc;
        }*/
        String fbgmx = fbgyy;
        jcysfbgjl.setFbgyy(fbgyy);
        jcysfbgjl.setFbgmx(fbgmx);

        jcysfbgjl.setCreateTime(new Date());
        jcysfbgjl.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        jcysfbgjl.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
        spcyRsJcysfbgjlMapper.insert(jcysfbgjl);
    }

    @Override
    public void initPrJcysgbgjl(String userid, String fjcyid, String fjcylb, String fjcyxl, String oldFssk, String newFssk, String fzytc, String fpid, String fwjh, boolean isReturn) {
        if (isReturn){
            spcyRsJcysfbgjlMapper.delete(new LambdaQueryWrapper<SpcyRsJcysfbgjl>()
                    .eq(SpcyRsJcysfbgjl::getFpid, fpid)
                    .eq(SpcyRsJcysfbgjl::getFbglx, JcyLxjhConstant.JCY_SFBG_BGLX_PR));

            return;
        }

        SpcyRsJcysfbgjl jcysfbgjl = new SpcyRsJcysfbgjl();
        jcysfbgjl.setId(GetUuIdUtils.ReplaceUuId());
        jcysfbgjl.setFjcyid(fjcyid);
        jcysfbgjl.setFuserid(userid);
        jcysfbgjl.setFpid(fpid);
        jcysfbgjl.setFbglx(JcyLxjhConstant.JCY_SFBG_BGLX_PR);
        jcysfbgjl.setFxllx(SpcyConstant.getJcyXllxByJcylb(fjcylb));
        jcysfbgjl.setFzytc(fzytc);

        if (StringUtils.isNotBlank(fjcyxl) && StringUtils.isNotBlank(fjcylb)){
            if (!fjcyxl.equals(fjcylb)){
                fjcylb = fjcylb+"("+fjcyxl+")";
            }
        }

        jcysfbgjl.setFjcylx(fjcylb);
        jcysfbgjl.setFwjh(fwjh);
        jcysfbgjl.setFbgsj(DateConversionUtils.DateToDays(new Date()));

        String fbgyy = "";

        String prlx = "";

        if (StringUtils.isBlank(oldFssk)){
            oldFssk = "无";
        }

        if (oldFssk.equals(SpcyConstant.HCY_SSK_YB)){
            prlx = "首聘";
        }else if (oldFssk.equals(SpcyConstant.HCY_SSK_ZY)){
            prlx = "晋升";
        }

        fbgyy += oldFssk + "-" + prlx + "-" + newFssk;

        /*String fbgyy = "（"+jcysfbgjl.getFbglx()+"）检查员类别-" + fjcyxl + ";";

        oldFssk = Optional.ofNullable(oldFssk).orElseGet(()->"无");
        fbgyy += "（"+oldFssk + "聘任为（" + newFssk + "）;";*/

        String fbgmx = fbgyy;
        jcysfbgjl.setFbgyy(fbgyy);
        jcysfbgjl.setFbgmx(fbgmx);

        jcysfbgjl.setCreateTime(new Date());
        jcysfbgjl.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        jcysfbgjl.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
        spcyRsJcysfbgjlMapper.insert(jcysfbgjl);
    }

    @Override
    public void initPrJcysgbgjl(String userid, String fjcyid, String fjcylb, String fjcyxl, String oldFssk, String newFssk, String fzytc, String fpid, String fwjh, boolean isReturn, String sfsqzh) {
        if (isReturn){
            spcyRsJcysfbgjlMapper.delete(new LambdaQueryWrapper<SpcyRsJcysfbgjl>()
                    .eq(SpcyRsJcysfbgjl::getFpid, fpid)
                    .eq(SpcyRsJcysfbgjl::getFbglx, JcyLxjhConstant.JCY_SFBG_BGLX_PR));

            return;
        }

        SpcyRsJcysfbgjl jcysfbgjl = new SpcyRsJcysfbgjl();
        jcysfbgjl.setId(GetUuIdUtils.ReplaceUuId());
        jcysfbgjl.setFjcyid(fjcyid);
        jcysfbgjl.setFuserid(userid);
        jcysfbgjl.setFpid(fpid);
        jcysfbgjl.setFbglx(JcyLxjhConstant.JCY_SFBG_BGLX_PR);
        jcysfbgjl.setFxllx(SpcyConstant.getJcyXllxByJcylb(fjcylb));
        jcysfbgjl.setFzytc(fzytc);

        if (StringUtils.isNotBlank(fjcyxl) && StringUtils.isNotBlank(fjcylb)){
            if (!fjcyxl.equals(fjcylb)){
                fjcylb = fjcylb+"("+fjcyxl+")";
            }
        }

        jcysfbgjl.setFjcylx(fjcylb);
        jcysfbgjl.setFwjh(fwjh);
        jcysfbgjl.setFbgsj(DateConversionUtils.DateToDays(new Date()));

        String fbgyy = "";

        String prlx = "";

        if (StringUtils.isBlank(oldFssk)){
            oldFssk = "无";
        }

        if (oldFssk.equals(SpcyConstant.HCY_SSK_YB)){
            prlx = "首聘";
        }else if (oldFssk.equals(SpcyConstant.HCY_SSK_ZY)){
            prlx = "晋升";
        }

        fbgyy += oldFssk + "-" + prlx + "-" + newFssk;

        /*String fbgyy = "（"+jcysfbgjl.getFbglx()+"）检查员类别-" + fjcyxl + ";";

        oldFssk = Optional.ofNullable(oldFssk).orElseGet(()->"无");
        fbgyy += "（"+oldFssk + "聘任为（" + newFssk + "）;";*/

        String fbgmx = fbgyy;
        jcysfbgjl.setFbgyy(fbgyy);
        jcysfbgjl.setFbgmx(fbgmx);

        jcysfbgjl.setFsfsqzh(sfsqzh);

        jcysfbgjl.setCreateTime(new Date());
        jcysfbgjl.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        jcysfbgjl.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
        spcyRsJcysfbgjlMapper.insert(jcysfbgjl);
    }

    @Async(value = "asyncMiddleTask")
    @Override
    public void initJpJcysgbgjl(String userid, String fjcyids, String fjcylb, String fjcyxls, String oldFssk, String fzytc, String fpid, String fwjh, boolean isHfjp) {
        if (isHfjp){
            spcyRsJcysfbgjlMapper.delete(new LambdaQueryWrapper<SpcyRsJcysfbgjl>()
                    .eq(SpcyRsJcysfbgjl::getFpid, fpid)
                    .eq(SpcyRsJcysfbgjl::getFbglx, JcyLxjhConstant.JCY_SFBG_BGLX_JP));

            return;
        }

        if (StringUtils.isBlank(fjcyids)){
            return;
        }
        List<String> jcyidsList = StringPatternUtils.getListByIds(fjcyids);
        List<String> jcyxlList = StringPatternUtils.getListByIds(fjcyxls);
        if (CollectionUtil.isEmpty(jcyidsList)){
            return;
        }
        for (int i = 0; i < jcyidsList.size(); i++) {

            String fjcyxl = jcyxlList.get(i);
            fjcylb = JcydjbConstant.getJcylbByJcyxl(fjcyxl);

            SpcyRsHcy jcy = spcyRsHcyMapper.selectById(jcyidsList.get(i));
            if (jcy != null){
                oldFssk = jcy.getFssk();
            }

            SpcyRsJcysfbgjl jcysfbgjl = new SpcyRsJcysfbgjl();
            jcysfbgjl.setId(GetUuIdUtils.ReplaceUuId());
            jcysfbgjl.setFjcyid(jcyidsList.get(i));
            jcysfbgjl.setFuserid(userid);
            jcysfbgjl.setFpid(fpid);
            jcysfbgjl.setFbglx(JcyLxjhConstant.JCY_SFBG_BGLX_JP);
            jcysfbgjl.setFxllx(SpcyConstant.getJcyXllxByJcylb(fjcylb));
            jcysfbgjl.setFzytc(fzytc);

            if (StringUtils.isNotBlank(fjcyxl) && StringUtils.isNotBlank(fjcylb)){
                if (!fjcyxl.equals(fjcylb)){
                    fjcylb = fjcylb+"("+fjcyxl+")";
                }
            }

            jcysfbgjl.setFjcylx(fjcylb);
            jcysfbgjl.setFwjh(fwjh);
            jcysfbgjl.setFbgsj(DateConversionUtils.DateToDays(new Date()));

            String fbgyy = "";

            if (!isHfjp) {
                fbgyy = "解聘-" + oldFssk;
            }

            String fbgmx = fbgyy;
            jcysfbgjl.setFbgyy(fbgyy);
            jcysfbgjl.setFbgmx(fbgmx);

            jcysfbgjl.setCreateTime(new Date());
            jcysfbgjl.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
            jcysfbgjl.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
            spcyRsJcysfbgjlMapper.insert(jcysfbgjl);
        }
    }

    @Override
    public void initSqJpJcysgbgjl(String userid, String fjcyid, String fjcylb, String fjcyxl, String oldFssk, String fzytc, String fpid, boolean isHfjp) {
        if (isHfjp) {
            spcyRsJcysfbgjlMapper.delete(new LambdaQueryWrapper<SpcyRsJcysfbgjl>()
                    .eq(SpcyRsJcysfbgjl::getFpid, fpid)
                    .eq(SpcyRsJcysfbgjl::getFbglx, JcyLxjhConstant.JCY_SFBG_BGLX_SQJP));

            return;
        }


        SpcyRsJcysfbgjl jcysfbgjl = new SpcyRsJcysfbgjl();
        jcysfbgjl.setId(GetUuIdUtils.ReplaceUuId());
        jcysfbgjl.setFjcyid(fjcyid);
        jcysfbgjl.setFuserid(userid);
        jcysfbgjl.setFpid(fpid);
        jcysfbgjl.setFbglx(JcyLxjhConstant.JCY_SFBG_BGLX_SQJP);
        jcysfbgjl.setFxllx(SpcyConstant.getJcyXllxByJcylb(fjcylb));
        jcysfbgjl.setFzytc(fzytc);

        if (StringUtils.isNotBlank(fjcyxl) && StringUtils.isNotBlank(fjcylb)) {
            if (!fjcyxl.equals(fjcylb)) {
                fjcylb = fjcylb + "(" + fjcyxl + ")";
            }
        }

        jcysfbgjl.setFjcylx(fjcylb);
//        jcysfbgjl.setFwjh(fwjh);
        jcysfbgjl.setFbgsj(DateConversionUtils.DateToDays(new Date()));

        String fbgyy = "";

        if (!isHfjp) {
            fbgyy = "解聘-" + oldFssk;
        }

        String fbgmx = fbgyy;
        jcysfbgjl.setFbgyy(fbgyy);
        jcysfbgjl.setFbgmx(fbgmx);

        jcysfbgjl.setCreateTime(new Date());
        jcysfbgjl.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        jcysfbgjl.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
        spcyRsJcysfbgjlMapper.insert(jcysfbgjl);
    }

    @Override
    public void initSqTxypJcysgbgjl(String userid, String fjcyid, String fjcylb, String fjcyxl, String oldFssk, String fzytc, String fpid, boolean isHfjp) {
        if (isHfjp) {
            spcyRsJcysfbgjlMapper.delete(new LambdaQueryWrapper<SpcyRsJcysfbgjl>()
                    .eq(SpcyRsJcysfbgjl::getFpid, fpid)
                    .eq(SpcyRsJcysfbgjl::getFbglx, JcyLxjhConstant.JCY_SFBG_BGLX_TXYP));

            return;
        }


        SpcyRsJcysfbgjl jcysfbgjl = new SpcyRsJcysfbgjl();
        jcysfbgjl.setId(GetUuIdUtils.ReplaceUuId());
        jcysfbgjl.setFjcyid(fjcyid);
        jcysfbgjl.setFuserid(userid);
        jcysfbgjl.setFpid(fpid);
        jcysfbgjl.setFbglx(JcyLxjhConstant.JCY_SFBG_BGLX_TXYP);
        jcysfbgjl.setFxllx(SpcyConstant.getJcyXllxByJcylb(fjcylb));
        jcysfbgjl.setFzytc(fzytc);

        if (StringUtils.isNotBlank(fjcyxl) && StringUtils.isNotBlank(fjcylb)) {
            if (!fjcyxl.equals(fjcylb)) {
                fjcylb = fjcylb + "(" + fjcyxl + ")";
            }
        }

        jcysfbgjl.setFjcylx(fjcylb);
//        jcysfbgjl.setFwjh(fwjh);
        jcysfbgjl.setFbgsj(DateConversionUtils.DateToDays(new Date()));

        String fbgyy = "";

        if (!isHfjp) {
            fbgyy = "退休延聘-" + oldFssk;
        }

        String fbgmx = fbgyy;
        jcysfbgjl.setFbgyy(fbgyy);
        jcysfbgjl.setFbgmx(fbgmx);

        jcysfbgjl.setCreateTime(new Date());
        jcysfbgjl.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        jcysfbgjl.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
        spcyRsJcysfbgjlMapper.insert(jcysfbgjl);
    }

}
